package com.GPT.common;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import java.util.Properties;

import static com.GPT.common.ValidateCodeUtils.generateValidateCode;

@Service
public class EmailService {

    private static JavaMailSender javaMailSender;

    public EmailService() {
        this.javaMailSender = createJavaMailSender();
    }

    private JavaMailSender createJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        // 设置邮件服务器属性
        mailSender.setHost("smtp.qq.com");
        mailSender.setPort(587);
        mailSender.setUsername("3497482950@qq.com"); // 替换为你的 QQ 邮箱
        mailSender.setPassword("dcclbfslkvbmdbhb"); // 替换为你的 QQ 邮箱应用专用密码

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        return mailSender;
    }

    public static void sendEmail(String toEmail, String verificationCode) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3497482950@qq.com"); // 设置发件人地址
        message.setTo(toEmail);//收件人
        message.setSubject("注册验证码");//标题
        message.setText("您的验证码是: " + verificationCode);//信息
        javaMailSender.send(message);
    }

//    public static void main(String[] args) {
//        sendEmail("2660986519@qq.com", String.valueOf(generateValidateCode(4)));
//    }
}
